package com.kin207.netty.msg.parse;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kin207.netty.msg.MessageParam;

public class FieldMessageParamUtil {
	final static Logger logger = LoggerFactory.getLogger(FieldMessageParamUtil.class);
	
	private Map<Class, List<FieldMessageParamIndex>> messageParamFields = new HashMap();
	
	public List<FieldMessageParamIndex> getMessageParamFields(Class clazz){
		List<FieldMessageParamIndex> fieldList = messageParamFields.get(clazz);
		if(fieldList==null){
			logger.debug("parse message, find {} MessageParam annotation cached fields", clazz);
			fieldList = new ArrayList();
			 Field[] fields = clazz.getDeclaredFields();
			 for(Field field : fields){
				 MessageParam mp = field.getAnnotation(MessageParam.class);
				 if(mp!=null){
					 field.setAccessible(true);
					 logger.debug("parse message, cached field:{}, index:{}", field, mp.index());
					 FieldMessageParamIndex mpi = new FieldMessageParamIndex(field, mp.index());
					 fieldList.add(mpi);
				 }
			 }
			messageParamFields.put(clazz, fieldList);
		}
		return fieldList;
	}
	

	public Map<Class, List<FieldMessageParamIndex>> getMessageParamFields() {
		return messageParamFields;
	}

	public void setMessageParamFields(Map<Class, List<FieldMessageParamIndex>> messageParamFields) {
		this.messageParamFields = messageParamFields;
	}
	
	
}
